// printRoom Mannamge
Array.prototype.unique = function() {
    var a = [],k = 0,e;
    for(k=0;e=this[k];k++)
      if(a.indexOf(e)==-1)
           a.push(e);
    return a;
}

function printRoomManagementCheckout(data2){
	var link = "/messageCheckOutService";
	var data = { func: "getMessageCheckOut"}
	simpleserviceWithArgs(link,data,messageCheckOutSuccess,data2);
}
function messageCheckOutSuccess(data,data2){
	if(data2==null){
		data2 = '{"id":"4719103906414592","guestId":"4859841394769920","cleaningStatus":"Clean Rooms","phone":"1","email":"1","pracketPrice":"Agoda Rate","pracketPriceId":"4591560557592576","tax":"300","totaltax":"900","afterNightChargeRoom":-1,"defaultCherge":"600","name":"1 1","balance":"899","groupId":"0","isAssign":"false","roomName":"114","roomTypeName":"iDeluxe Queen Bed","roomNameId":"4890627720347648","roomTypeId":"6227633859723264","date":"Mar 5, 2014 12:00:00 AM","span":"1","order":"1","color":"#994499","isChangeRoomFrom":"false","isChangeRoomTo":"false","dateTo":"Mar 6, 2014 12:00:00 AM","reservationTitle":"Reserved Rooms","sellRoom":"false","paymentList":"900.0","adult":"1","description":"","paymentAmount":"1.0","ChargeCheckTemp":"0.0","typePage":"All","child":"0"}';
		data2 = JSON.parse(data2);
	}
	objArray = data2;
	objArray2 = JSON.parse(data);
	var groupIdDisplay = "";
	var RoomNight = "";
	var Night = 0;
	var roomType = "";
	var adult = 0;
	var packetPrice = "";
	var defalutCharge = 0;
	var tax = 0;
	var priceSumTax = 0;
	var paymentAmount = 0;
	var balance = 0;
	var reservationOrGroup = "";
	if(objArray.groupId!="0"){
		var roomTypeArray = new Array();
		var packetPriceArray = new Array();
		groupIdDisplay  = "Group ID # G"+objArray.groupId;
		if(objArray.subList!=undefined){
			for(var i =0;i<objArray.subList.length;i++){
				roomTypeArray.push(objArray.subList[i].roomTypeName);
				packetPriceArray.push(objArray.subList[i].pracketPrice);
				RoomNight += '<font size="-1">'+objArray.subList[i].roomName+'</font><font size="-2">('+objArray.subList[i].span+'N)</font>,';
				Night = parseInt(objArray.subList[i].span)+Night;
				adult = parseInt(objArray.subList[i].adult)+parseInt(objArray.subList[i].child)+adult;
				defalutCharge = parseInt(objArray.subList[i].defaultCherge)+defalutCharge;
				tax = parseInt(objArray.subList[i].tax)+tax;
				priceSumTax = parseInt(objArray.subList[i].totaltax)+priceSumTax;
				paymentAmount = parseInt(objArray.subList[i].paymentAmount)+paymentAmount;
				balance = parseInt(objArray.subList[i].balance)+balance;
			}
			RoomNight = RoomNight.substring(0, RoomNight.length-1);
			roomType = roomTypeArray.unique().toString();
			packetPrice = packetPriceArray.unique().toString();
			reservationOrGroup = '<td><h4> '+groupIdDisplay+'</h4></td></tr></tbody>';
		}else{
			RoomNight = '<font size="-1">'+objArray.roomName+'</font><font size="-2">('+objArray.span+'N)</font>';
			roomType = objArray.roomTypeName;
			Night = objArray.span;
			adult = parseInt(objArray.adult)+parseInt(objArray.child);
			packetPrice = objArray.pracketPrice;
			defalutCharge = objArray.defaultCherge;
			tax = objArray.tax;
			priceSumTax = objArray.totaltax;
			paymentAmount = objArray.paymentAmount;
			balance = objArray.balance;
			reservationOrGroup = '<td><h4> '+groupIdDisplay+'&nbsp;&nbsp;&nbsp;Reservation ID # '+objArray.id+'</h4></td></tr></tbody>';
		}
	}else{
		RoomNight = '<font size="-1">'+objArray.roomName+'</font><font size="-2">('+objArray.span+'N)</font>';
		roomType = objArray.roomTypeName;
		Night = objArray.span;
		adult = parseInt(objArray.adult)+parseInt(objArray.child);
		packetPrice = objArray.pracketPrice;
		defalutCharge = objArray.defaultCherge;
		tax = objArray.tax;
		priceSumTax = objArray.totaltax;
		paymentAmount = objArray.paymentAmount;
		balance = objArray.balance;
		reservationOrGroup = '<td><h4>Reservation ID # '+objArray.id+'</h4></td></tr></tbody>';
	}
	myWindow=window.open('','','width=783,height=722');
	var headHtml = '<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' +
				    '<title>Room Reservation Check In</title>' +
				    '<script src="js/jquery-1.8.3.min.js"></script>'+
				    '<link href="https://695.staygrid.com/styles/billmsg.css" rel="stylesheet" type="text/css">' +
				    '<style media="print">' +
				    '.dont-print-buttom{' +
				    'display:none;' +
				    '}' +
				    'P.pagebreakhere {page-break-before: always}' +
				    '</style>'+
				    '<script>'+
				    "var display = 0;"+
				    'function showPrice(){'+
					    'if(display == 0){'+
					    	'$("#displayPrice").attr("style","display:none"); $("#displayPrice2").attr("style","display:show"); display=1;'+
					    '}else{'+
					    	'$("#displayPrice").attr("style","display:show"); $("#displayPrice2").attr("style","display:none"); display=0;'+
					    '}'+
				    '}'+
				    '</script>'+
				    '</head><body>';
	var hotel = '<table  border="0" align="center" cellspacing="0" cellpadding="0" style="width:650px;">' +
			    '<tbody>' +
			    '<tr>' +
			    '<td width="65%">' +
			    '<img src="https://s3.amazonaws.com/livestaygriddata/product-images/hotels/logo/logo08042013072057140130407.jpeg" width="152.36276849642" height="160">' +
			    '</td>' +
			    '<td width="35%">' +
			    '<table width="100%">' +
			    '<tbody>' +
			    '<tr>' +
			    '<td>' +
			    '<table style="font-size:11px" width="100%" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td colspan="2" style="font-size:14px"><strong>iHotel</strong></td></tr><tr><td colspan="2" width="100%" valign="top">......................................<br>Thakor - 48000<br>Nakhon Phanom, Thailand</td></tr><tr><td width="10%"><strong>Phone:</strong></td><td width="90%">+6642543355</td></tr><tr><td><strong>Fax:</strong></td><td>+6642543377</td></tr><tr><td><strong>Email:</strong></td><td>ihotelthailands@gmail.com</td></tr><tr><td><strong>Website:&nbsp;</strong></td><td>www.ihotel.co.th</td></tr></tbody></table>' +
			    '</td>' +
			    '</tr>' +
			    '</tbody>' +
			    '</table>' +
			    '</td>' +
			    '</tr>' +
			    '</tbody>' +
			    '</table>';
	var dateIn = new Date(objArray.date);
	var dateTo = new Date(objArray.dateTo)
	var formatDateIn = dateIn.getFormatedString();
	var formatDateTo = dateTo.getFormatedString();
	var gusetId = "";
	var name = "";
	var phone = "";
	if(objArray.guestId!=0){gusetId = objArray.guestId;}
	if(objArray.name!=undefined){name = objArray.name;}
	if(objArray.phone!=undefined){phone = objArray.phone;}
	var dateNow = new Date();
	var formatDateNow = dateNow.getFormatedString();
	var dateCheckin = new Date(objArray.date);
	var formatDateCheckin = dateCheckin.getFormatedString();
	var dateCheckout = new Date(objArray.dateTo);
	var formatDateCheckout = dateCheckout.getFormatedString();
	var strPaymentList = "";
	var strChargeList = "";
	var strchargeOtherList = "";
	if(objArray.subList!=null){
		var objSubList = objArray.subList;
		for(var i =0;i<objSubList.length;i++){
			var chargeList = objSubList[i].ChargeList;
			var chargeOtherList = objSubList[i].OtherChargeList;
			var objCharge = chargeSet(chargeList,chargeOtherList);
			strChargeList += objCharge.strChargeList;
			strchargeOtherList += objCharge.strchargeOtherList;
			
		}
		var objFolioId = objArray.folioId;
	}else{
		var chargeList = objArray.ChargeList;
		var chargeOtherList = objArray.OtherChargeList;
		var objCharge = chargeSet(chargeList,chargeOtherList);
		var objFolioId = objArray.folioId;
		strChargeList = objCharge.strChargeList;
		strchargeOtherList = objCharge.strchargeOtherList;
	}
	var Comfort = '<table style="font-size:11px" border="0" align="center" cellpadding="10" cellspacing="0" bgcolor="#FFFFFF" width="100%">' +
				    '<tbody><tr>' +
				    '<td  width="100%" colspan="2">'+objArray2.message+'</td></tr></tbody></table><br><br>';
	var reservationId = '<div id="details" style="line-height:15px; width:200px; font-size:11px;"><strong>Reservation ID&nbsp;&nbsp;'+objArray.id+
						'</strong><br><strong>Folio # '+objFolioId+' <br>'+name+' (ID	# P'+gusetId+')</strong><br></div>';
	var room = '<div id="room">'+
			    '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="line-height:15px; font-size:11px;">'+
			    '<tbody>'+
			       '<tr>'+
			            '<td width="38%"><strong>Room No</strong>'+
			            '</td>'+
			            '<td width="9%" align="center">:</td>'+
			            '<td width="53%">'+RoomNight+'</td>'+
			        '</tr>'+
			        '<tr>'+
			            '<td><strong>Pax</strong>'+
			            '</td>'+
			            '<td align="center">:</td>'+
			            '<td>'+adult+' Adults</td>'+
			        '</tr>'+
			        '<tr>'+
			            '<td><strong>Arrival</strong>'+
			            '</td>'+
			            '<td align="center">:</td>'+
			            '<td>'+formatDateCheckin+'</td>'+
			        '</tr>'+
			        '<tr>'+
			            '<td><strong>Departure</strong>'+
			            '</td>'+
			            '<td align="center">:</td>'+
			            '<td>'+formatDateCheckout+'</td>'+
			        '</tr>'+
			    '</tbody>'+
			'</table>'+
			'</div>';
	if(objArray.balance!=null)
		var balance = objArray.balance;
	else
		var balance = "0";
	if(objArray.chargeFolio)
		var chargeFolio = objArray.chargeFolio;
	else
		var chargeFolio = "0";
	if(objArray.paidFolio!=null)
		var paymentFolio = objArray.paidFolio;
	else
		var paymentFolio = "0";
	var table = '<table border="0" align="center" cellspacing="0" cellpadding="0" style="width:650px;">'+
			    '<tbody>'+
			    '<tr>'+
			        '<td align="left">'+
			       			Comfort+
			        		reservationId+
			        		room+
			        '</td>'+
			    '</tr>'+
			    '<tr>'+
			        '<td align="center" valign="top">&nbsp;</td>'+
			    '</tr>'+
			    '<tr>'+
			        '<td class="bg-line"></td>'+
			    '</tr>'+
			    '<tr>'+
			        '<td align="center" valign="top">'+
			            '<table width="100%" border="0" cellspacing="8" cellpadding="0" style="font-size:11px;">'+
			                '<tbody>'+
			                    '<tr>'+
			                        '<td width="16%" align="center"><strong>Date(s)</strong>'+
			                        '</td>'+
			                        '<td width="34%" align="left"><strong>Description</strong>'+
			                        '</td>'+
			                        '<td width="26%" align="right" colspan="2"><strong>References</strong>'+
			                        '</td>'+
			                        '<td width="10%" align="right"><strong>&nbsp;</strong>'+
			                        '</td>'+
			                        '<td width="14%" align="right"><strong>Amount</strong>'+
			                        '</td>'+
			                    '</tr>'+
				                '<tr>'+
				                	'<td height="1" colspan="6" bgcolor="#000000"></td>'+
				                '</tr>'+
				                '<tr>'+
				                	'<td colspan="6">&nbsp;</td>'+
				                '</tr>'+
									strChargeList+	
									strchargeOtherList+
				                '<tr>'+
			                		'<td colspan="6">&nbsp;</td>'+
			                	'</tr>'+
			                    '<tr>'+
			                        '<td height="1" colspan="6" bgcolor="#000000"></td>'+
			                    '</tr>'+
			                    '<tr>'+
			                        '<td colspan="2"></td>'+
			                        '<td height="30" colspan="2" align="right" class="size16"><strong>Total Amount</strong>'+
			                        '</td>'+
			                        '<td colspan="2" align="right" class="size16"><strong>'+toCurrencyString(currencyStringToNumber(chargeFolio))+'</strong>'+
			                        '</td>'+
			                    '</tr>'+
			                    '<tr>'+
			                        '<td colspan="2"></td>'+
			                        '<td height="30" colspan="2" align="right" class="size16">Includes Tax</td>'+
			                        '<td colspan="2" align="right" class="size16">'+
			                            '฿&nbsp;0.00</td>'+
			                    '</tr>'+
			                    '<tr>'+
			                        '<td colspan="2"></td>'+
			                        '<td height="30" colspan="2" align="right" class="size16"><strong>Amount Paid</strong>'+
			                        '</td>'+
			                        '<td colspan="2" align="right" class="size16"><strong>'+toCurrencyString(currencyStringToNumber(paymentFolio))+'</strong>'+
			                        '</td>'+
			                    '</tr>'+
			                    '<tr>'+
			                        '<td colspan="2"></td>'+
			                        '<td height="3" colspan="4" bgcolor="#000000"></td>'+
			                    '</tr>'+
			                    '<tr>'+
			                        '<td colspan="2"></td>'+
			                        '<td height="30" colspan="2" align="right" class="size16"><strong>Balance</strong>'+
			                        '</td>'+
			                        '<td colspan="2" align="right" class="size16"><strong>'+toCurrencyString(currencyStringToNumber(balance))+'</strong>'+
			                        '</td>'+
			                    '</tr>'+
			                '</tbody>'+
			            '</table>'+
			        '</td>'+
			    '</tr>'+
			'</tbody>'+
			'</table>';
	var bottomTable = '<table align="center" border="0" cellpadding="5" cellspacing="0" style="width:650px;font-size:11px;">'+
					    '<tbody>'+
					    '<tr><td colspan="4">&nbsp;</td></tr>'+
					    '<tr><td colspan="4" align="left">'+objArray2.thxmessage+'</td></tr>'+
					    '<tr><td colspan="4" align="left">&nbsp;</td></tr>'+
					    '<tr><td colspan="4" align="left">'+
					    		'<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" style="font-size:11px;">'+
					                '<tbody>'+
					                    '<tr><td width="67%" class="lineheight20">--------------------------------------------------------------'+
					                            '<br>'+"Guest's Signature"+
					                            '<br>'+
					                            '<span class="size11">(Please collect the receipt when paying by cash</span>'+
					                            '<br>'+
					                            '<strong>Date:</strong> Monday, March 10, 2014'+
					                        '</td>'+
					                        '<td width="33%" valign="top">--------------------------------------------------------------'+
					                            '<br>'+"Cashier's Signature"+
					                        '</td>'+
					                    '</tr>'+
					                '</tbody>'+
					            '</table>'+
					        '</td>'+
					    '</tr>'+
					'</tbody>'+
					'</table>';
	var btnbot = '<form action="https://695.staygrid.com/admine/billmessage/preview/billType/I/id/2617/groupId/147993/rsvId/0/fromTape/1" method="post" name="shFrm">'+
	 '<input type="hidden" name="hidePriceReq" value="1">'+
	 '<table align="center" width="100%">'+
	 '<tbody><tr>'+
	 '<td align="center">'+
	 '<input name="btnClose" type="button" onclick="window.close();" value="Close" class="dont-print-buttom">'+
	 '<input name="btnPrint" type="button" onclick="window.print();" value="Print" class="dont-print-buttom">'+
	 '</td></tr></tbody></table></form> ';
	myWindow.document.write(headHtml+hotel+table+bottomTable+btnbot+'</body></html>');
	myWindow.focus();
}


function chargeSet(chargeList,chargeOtherList){
	var strChargeList ="";
	var strchargeOtherList ="";
	if(chargeList!=null)
		for(var i =0;i<chargeList.length;i++){
			var dateSet = new Date(chargeList[i].chargeDate);
			var formatDateIn = dateSet.getFormatedString();
			var descriptiont = chargeList[i].description;
			if(chargeList[i].chargeAmount!=null)
				var cost = toCurrencyString(currencyStringToNumber(chargeList[i].chargeAmount));
			else
				var cost = "";
			strChargeList += '<tr>'+
						         '<td>'+formatDateIn+'</td>'+
						         '<td>'+descriptiont+'</td>'+
						         '<td align="right" colspan="2"></td>'+
						         '<td align="right">&nbsp;</td>'+
						         '<td align="right">'+
						         	cost+
						         '</td>'+
							     '</tr>'+
							     '<tr>'+
			                		'<td colspan="6">&nbsp;</td>'+
			                	 '</tr>';
		}
	if(chargeOtherList!=null)
		for(var i =0;i<chargeOtherList.length;i++){
			var dateSet = new Date(chargeOtherList[i].chargeDate);
			var formatDateIn = dateSet.getFormatedString();
			var descriptiont = chargeOtherList[i].description;
			var cost = toCurrencyString(currencyStringToNumber(chargeOtherList[i].chargeAmount));
			strchargeOtherList += '<tr>'+
						         '<td>'+formatDateIn+'</td>'+
						         '<td>'+descriptiont+'</td>'+
						         '<td align="right" colspan="2"></td>'+
						         '<td align="right">&nbsp;</td>'+
						         '<td align="right">'+
						         	cost+
						         '</td>'+
							     '</tr>'+
							     '<tr>'+
			                		'<td colspan="6">&nbsp;</td>'+
			                	 '</tr>';
		}
	return {strChargeList:strChargeList,strchargeOtherList:strchargeOtherList}
}